package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.pojo.param.BrandAddNewParam;
import cn.tedu.csmall.product.pojo.param.BrandUpdateInfoParam;
import cn.tedu.csmall.product.pojo.vo.BrandListItemVO;
import cn.tedu.csmall.product.pojo.vo.BrandStandardVO;
import cn.tedu.csmall.product.service.IBrandService;
import com.example.csmall.commons.pojo.vo.PageData;
import com.example.csmall.commons.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/brand")
@Api(tags = "03.品牌管理模块")
@Slf4j
@Validated
public class BrandController {
    @Autowired
    private IBrandService brandService;

    @PostMapping("/add-new")
    @ApiOperation("添加品牌")
    @ApiOperationSupport(order = 100)

    public JsonResult addNew(@Valid BrandAddNewParam brandAddNewParam) {
        log.warn("开始处理【添加品牌】,参数:{}",brandAddNewParam);
        brandService.addNew(brandAddNewParam);

        return JsonResult.ok();
    }
    @PostMapping("/delete")
    @ApiOperation("根据ID删除品牌")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "品牌ID", required = true, dataType = "long")})
    public JsonResult delete(@Range(min = 1, message = "根据ID删除品牌失败,请提交合规的ID值")
                          @RequestParam Long id) throws Exception{
        log.warn("开始处理【根据ID删除品牌】,参数:{}", id);
        brandService.delete(id);
        return JsonResult.ok();
    }

    // http://localhost:9180/brand/id
    @GetMapping("/{id:[0-9]+}")
    @PreAuthorize("hasAuthority('/pms/brand/read')")
    @ApiOperation("根据ID查询品牌详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "品牌ID", required = true, dataType = "long")})
    public JsonResult getStandardById(@Range(min = 1, message = "根据ID查询品牌详情失败,请提交合规的ID值")
                                      @PathVariable Long id) {
        log.warn("开始处理【根据ID查询品牌详情】,参数:{}", id);
        BrandStandardVO result = brandService.getStandardById(id);
        return JsonResult.ok(result);
    }

    @PostMapping("/update")
    @ApiOperation("修改品牌详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "品牌ID", required = true, dataType = "long")})
    public JsonResult update(@Valid
                             @Range(min = 1, message = "修改品牌失败,请提交合规的ID值")
                             @RequestParam Long id, BrandUpdateInfoParam brandUpdateInfoParam) {
        log.warn("开始处理【修改品牌详情】,参数:{}",id);
        log.warn("修改后的参数: {}", brandUpdateInfoParam);
        brandService.updateInfoById(id, brandUpdateInfoParam);
        return JsonResult.ok();
    }

    @GetMapping("")
    @PreAuthorize("hasAuthority('/pms/brand/read')")
    @ApiOperation("查询品牌列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query")
    })
    public JsonResult list(@Range(min = 1, message = "查询品牌列表失败，请提供正确的页码值！") Integer page, String queryType) {
        log.debug("开始处理【查询品牌列表】的请求，页码：{}", page);
        Integer pageNum = page == null ? 1 : page;
        PageData<BrandListItemVO> pageData;
        if ("all".equals(queryType)) {
            pageData = brandService.list(pageNum, Integer.MAX_VALUE);
        } else {
            pageData = brandService.list(pageNum);
        }
        return JsonResult.ok(pageData);
    }
    
}
